package com.data.modules.online.cgreport.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import com.data.modules.base.service.BaseCommonService;
import com.data.modules.online.cgform.util.CgformUtil;
import com.data.modules.online.cgreport.model.OnlCgreportModel;
import com.data.modules.online.cgreport.common.CgReportConstant;
import io.swagger.annotations.Api;
import org.apache.commons.lang.StringUtils;
import com.data.common.api.vo.Result;
import com.data.common.system.api.ISysBaseAPI;
import com.data.common.system.query.QueryGenerator;
import com.data.common.util.SqlInjectionUtil;
import com.data.modules.online.cgreport.entity.OnlCgreportHead;
import com.data.modules.online.cgreport.entity.OnlCgreportItem;
import com.data.modules.online.cgreport.entity.OnlCgreportParam;
import com.data.modules.online.cgreport.service.IOnlCgreportHeadService;
import com.data.modules.online.cgreport.service.IOnlCgreportItemService;
import com.data.modules.online.cgreport.service.IOnlCgreportParamService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping({"/online/cgreport/head"})
@RestController
@Api(tags="onlCgreportHeadController")
public class OnlCgreportHeadController {

    private static final Logger f344a = LoggerFactory.getLogger(OnlCgreportHeadController.class);
    @Autowired
    private BaseCommonService baseCommonService;
    @Autowired
    private IOnlCgreportHeadService onlCgreportHeadService;
    @Autowired
    private IOnlCgreportItemService onlCgreportItemService;
    @Autowired
    private IOnlCgreportParamService onlCgreportParamService;
    @Autowired
    private ISysBaseAPI sysBaseAPI;

    @GetMapping({"/parseSql"})
    public Result<?> mo1027a(@RequestParam(name = "sql") String str, @RequestParam(name = "dbKey", required = false) String str2) {
        String str3;
        if (StringUtils.isNotBlank(str2) && this.sysBaseAPI.getDynamicDbSourceByCode(str2) == null) {
            return Result.error("数据源不存在");
        }
        HashMap hashMap = new HashMap();
        ArrayList arrayList = new ArrayList();
        ArrayList arrayList2 = new ArrayList();
        try {
            this.baseCommonService.addLog("Online报表，sql解析：" + str, 2, 2);
            SqlInjectionUtil.specialFilterContentForOnlineReport(str);
            List<String> sqlFields = this.onlCgreportHeadService.getSqlFields(str, str2);
            List<String> sqlParams = this.onlCgreportHeadService.getSqlParams(str);
            int i = 1;
            for (String str4 : sqlFields) {
                OnlCgreportItem onlCgreportItem = new OnlCgreportItem();
                onlCgreportItem.setFieldName(str4.toLowerCase());
                onlCgreportItem.setFieldTxt(str4);
                onlCgreportItem.setIsShow(1);
                onlCgreportItem.setOrderNum(Integer.valueOf(i));
                onlCgreportItem.setId(CgformUtil.getId());
                onlCgreportItem.setFieldType(CgReportConstant.f379z);
                arrayList.add(onlCgreportItem);
                i++;
            }
            for (String str5 : sqlParams) {
                OnlCgreportParam onlCgreportParam = new OnlCgreportParam();
                onlCgreportParam.setParamName(str5);
                onlCgreportParam.setParamTxt(str5);
                arrayList2.add(onlCgreportParam);
            }
            hashMap.put("fields", arrayList);
            hashMap.put(CgReportConstant.f356c, arrayList2);
            return Result.ok(hashMap);
        } catch (Exception e) {
            f344a.error(e.getMessage(), e);
            if (e.getMessage().indexOf("Connection refused: connect") != -1) {
                str3 = "解析失败，" + "数据源连接失败.";
            } else if (e.getMessage().indexOf("值可能存在SQL注入风险") != -1) {
                str3 = "解析失败，" + "SQL可能存在SQL注入风险.";
            } else if (e.getMessage().indexOf("该报表sql没有数据") != -1) {
                str3 = "解析失败，" + "报表sql查询数据为空，无法解析字段.";
            } else if (e.getMessage().indexOf("SqlServer不支持SQL内排序") != -1) {
                str3 = "解析失败，" + "SqlServer不支持SQL内排序.";
            } else {
                str3 = "解析失败，" + "SQL语法错误.";
            }
            return Result.error(str3);
        }
    }

    @GetMapping({"/list"})
    public Result<IPage<OnlCgreportHead>> mo1028a(OnlCgreportHead onlCgreportHead, @RequestParam(defaultValue = "1", name = "pageNo") Integer num, @RequestParam(defaultValue = "10", name = "pageSize") Integer num2, HttpServletRequest httpServletRequest) {
        Result<IPage<OnlCgreportHead>> result = new Result<>();
        Wrapper initQueryWrapper = QueryGenerator.initQueryWrapper(onlCgreportHead, httpServletRequest.getParameterMap());
        IPage page = this.onlCgreportHeadService.page(new Page((long) num.intValue(), (long) num2.intValue()), initQueryWrapper);
        result.setSuccess(true);
        result.setResult(page);
        return result;
    }

    @PostMapping({"/add"})
    public Result<?> mo1029a(@RequestBody OnlCgreportModel onlCgreportModel) {
        Result<?> result = new Result<>();
        try {
            String a = CgformUtil.getId();
            OnlCgreportHead head = onlCgreportModel.getHead();
            List<OnlCgreportParam> params = onlCgreportModel.getParams();
            List<OnlCgreportItem> items = onlCgreportModel.getItems();
            head.setId(a);
            for (OnlCgreportParam onlCgreportParam : params) {
                onlCgreportParam.setId(null);
                onlCgreportParam.setCgrheadId(a);
            }
            for (OnlCgreportItem onlCgreportItem : items) {
                onlCgreportItem.setId(null);
                onlCgreportItem.setFieldName(onlCgreportItem.getFieldName().trim().toLowerCase());
                onlCgreportItem.setCgrheadId(a);
            }
            this.onlCgreportHeadService.save(head);
            this.onlCgreportParamService.saveBatch(params);
            this.onlCgreportItemService.saveBatch(items);
            result.success("添加成功！");
        } catch (Exception e) {
            f344a.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    @CacheEvict(allEntries = true, beforeInvocation = true, value = {"sys:cache:online:rp"})
    @PutMapping({"/editAll"})
    public Result<?> mo1031b(@RequestBody OnlCgreportModel onlCgreportModel) {
        try {
            return this.onlCgreportHeadService.editAll(onlCgreportModel);
        } catch (Exception e) {
            f344a.error(e.getMessage(), e);
            return Result.error("操作失败");
        }
    }

    @DeleteMapping({"/delete"})
    public Result<?> mo1026a(@RequestParam(name = "id", required = true) String str) {
        return this.onlCgreportHeadService.delete(str);
    }

    @DeleteMapping({"/deleteBatch"})
    public Result<?> mo1030b(@RequestParam(name = "ids", required = true) String str) {
        return this.onlCgreportHeadService.bathDelete(str.split(CgformUtil.f231E));
    }

    @GetMapping({"/queryById"})
    public Result<OnlCgreportHead> mo1032c(@RequestParam(name = "id", required = true) String str) {
        Result<OnlCgreportHead> result = new Result<>();
        result.setResult((OnlCgreportHead) this.onlCgreportHeadService.getById(str));
        return result;
    }
}
